var prefix = jsctx + "/hlhtTenantOpenList";

var hlhtTenantOpen = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;
    function load() {
        var tenantCode = $("#tenantCode").val();
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageHlhtTenantOpen",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    height:h,
                    pageList: [20],
                    pageNumber: 1,
                    sidePagination: "server",
                    queryParams: function (params) {
                        return {
                            hlhtOpenType: $("#hlhtOpenType").val(),
                            hlhtType: $("#hlhtType").val(),
                            status: $("#status").val(),
                            hlhtTenantOrHlhtOperator: $("#hlhtTenantOrHlhtOperator").val(),
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '30',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {
                            field: 'tenantCode', title: '本方编号', align: 'left', width: '80',
                            formatter: function (value, row, index) {
                                if (row.tenantCode == tenantCode) {
                                    return row.tenantCode;
                                } else {
                                    return row.hlhtTenantCode;
                                }
                            }

                        },
                        {
                            field: 'tenantName', title: '本方运营商', align: 'left', width: '100',
                            formatter: function (value, row, index) {
                                if (row.tenantCode == tenantCode) {
                                    return row.tenantName;
                                } else {
                                    return row.hlhtTenantName;
                                }
                            }

                        },
                        {
                            field: 'code', title: '对方编号', align: 'left', width: '80',
                            formatter: function (value, row, index) {
                                if (row.tenantCode == tenantCode) {
                                    if (row.hlhtOpenType == '0') {
                                        return row.hlhtTenantCode;
                                    } else {
                                        return row.hlhtOperatorId;
                                    }
                                } else {
                                    return row.tenantCode;
                                }
                            }
                        },
                        {
                            field: 'name', title: '对方运营商', align: 'left', width: '100',
                            formatter: function (value, row, index) {
                                if (row.tenantCode == tenantCode) {
                                    if (row.hlhtOpenType == '0') {
                                        return row.hlhtTenantName;
                                    } else {
                                        return row.hlhtOperatorName;
                                    }
                                } else {
                                    return row.tenantName;
                                }
                            }
                        },
                        {
                            field: 'brand', title: '互联品牌', align: 'left', width: '50',
                            formatter: function (value, row, index) {
                                if (row.tenantCode == tenantCode) {
                                    if (row.hlhtOpenType == '0') {
                                        return row.hlhtTenantShortname;
                                    } else {
                                        return row.hlhtOperatorShortname;
                                    }
                                } else {
                                    return row.tenantShortname;
                                }
                            }
                        },
                        {field: 'beginDate', title: '互联开始时间', align: 'left', width: '100'},
                        {field: 'endDate', title: '互联结束时间', align: 'left', width: '100'},
                        {
                            field: 'hlhtOpenType', title: '互联类型', align: 'left', width: '50',
                            formatter: function (value, row, index) {
                                if (row.hlhtOpenType == '0') {
                                    return '内部互联';
                                } else if (row.hlhtOpenType == '1') {
                                    return '外部互联';
                                } else if (row.hlhtOpenType == '2') {
                                    return '政务互联';
                                }
                            }
                        },
                        {
                            field: 'hlhtType', title: '互联方向', align: 'left', width: '50',
                            formatter: function (value, row, index) {
                                if (row.tenantCode == tenantCode) {
                                    if (row.hlhtType == '0') {
                                        return '反向';
                                    } else if (row.hlhtType == '1') {
                                        return '正向';
                                    } else if (row.hlhtType == '2') {
                                        return '双向';
                                    }
                                } else {
                                    if (row.hlhtType == '0') {
                                        return '正向';
                                    } else if (row.hlhtType == '1') {
                                        return '反向';
                                    } else if (row.hlhtType == '2') {
                                        return '双向';
                                    }
                                }
                            }
                        },
                        {
                            field: 'status', title: '互联状态', align: 'left', width: '50',
                            formatter: function (value, row, index) {
                                if (row.status == '0') {
                                    return '已停用';
                                } else if (row.status == '1') {
                                    return '互联中';
                                } else if (row.status == '2') {
                                    return '已过期';
                                }

                            }
                        },
                        {
                            title: '操作',
                            field: 'roleId',
                            align: 'center',
                            width: '200',
                            formatter: function (value, row, index) {
                                var d = '';
                                var e = '';
                                var f = '';
                                var g = '';
                                var h = '';
                                if (Hlht_update){
                                    e = '<a class="btn btn-warning btn-sm ' + has_edit + '" href="#" title="站点开放管理" onclick="hlhtTenantOpen.stationHlhtTenantOpen(\''
                                        + row.tenantOpenId + '\',\'' + row.tenantCode
                                        + '\')">站点开放管理</a> ';
                                    f = '<a class="btn btn-info btn-sm ' + has_edit + '" href="#" title="互联折扣设置" onclick="hlhtTenantOpen.hlhtDiscount(\''
                                        + row.hlhtOperatorId + '\',\'' + row.tenantCode + '\',\'' + row.hlhtTenantCode
                                        + '\')">互联折扣设置</a> ';
                                    h = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="前端互联控制" onclick="hlhtTenantOpen.stationOpenfor(' + row.tenantCode + ',\'' +
                                        row.hlhtOperatorId + '\')">前端互联控制</a> ';
                                    g = '<a class="btn btn-warning btn-sm ' + has_edit + '" href="#" title="站点开放管理" onclick="hlhtTenantOpen.positiveHlhtStaion('
                                        + row.tenantOpenId + ')">站点开放管理</a> ';
                                }
                                if (Hlht_apply_look){
                                    d = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="订单查询" onclick="hlhtTenantOpen.hlhtOrderInfo(' + row.tenantOpenId + ',\'' +
                                        row.tenantCode + '\',\'' + row.hlhtTenantCode + '\',\'' + row.hlhtOperatorId + '\',\'' + row.hlhtType + '\')">查询订单</a> ';
                                }
                                if (row.hlhtOpenType == 2) {
                                    return e;
                                }

                                if (row.hlhtType == 0) {
                                    if (row.tenantCode == tenantCode) {
                                        return e + d + f;
                                    } else {
                                        return e + d;
                                    }
                                }

                                if (row.hlhtType == 1) {
                                    if (row.hlhtOperatorId != null && row.hlhtOperatorId != '') {
                                        return g + d + h;
                                    } else {
                                        return g + d;
                                    }

                                }
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function detailhlhtTenantOpen(tenantOpenId, isEdit) {
        location.href = prefix + "/detailHlhtTenantOpen?tenantOpenId=" + tenantOpenId + "&isEdit=" + isEdit;
    }

    function hlhtOrderInfo(tenantOpenId, tenantCode, hlhtTenantCode, hlhtOperatorId, hlhtType) {
        var id = "";
        if (hlhtTenantCode == null) {
            id = hlhtOperatorId;
        } else {
            id = hlhtTenantCode
        }
        location.href = prefix + "/hlhtOrderInfo?tenantOpenId=" + tenantOpenId + "&tenantCode=" + tenantCode + "&hlhtTenantCode=" + id + "&hlhtOperatorId=" + id
            + "&hlhtType=" + hlhtType;
    }

    function updateTenantOpenStatus(tenantOpenId, status) {
        $.ajax({
            url: prefix + "/updateHlhtTenantOpenStatus",
            type: "post",
            data: {
                'tenantOpenId': tenantOpenId,
                'status': status,
            },
            success: function (data) {

                if (data.status == 200) {
                    layer.msg("操作成功");
                    reLoad();
                } else {
                    layer.msg(data.message);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                layer.msg(textStatus + errorThrown);
            }
        });
    }

    function stationHlhtTenantOpen(tenantOpenId, tenantCode) {
        location.href = prefix + "/stationHlhtTenantOpen?tenantOpenId=" + tenantOpenId + "&tenantCode=" + tenantCode;
    }

    function positiveHlhtStaion(tenantOpenId) {
        location.href = prefix + "/positiveHlhtStaion?tenantOpenId=" + tenantOpenId;
    }

    function stationOpenfor(tenantCode, hlhtOperatorId) {
        location.href = prefix + "/stationOpenfor/" + tenantCode + "/" + hlhtOperatorId;
    }

    function goBack() {
        window.history.back();
    }

    function saveBatchHlhtTenantOpenStation(superLevelType) {
        layer.confirm('确定批量开放？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                url: prefix + "/saveBatchHlhtTenantOpenStation",
                type: "post",
                data: {
                    'superLevelType': superLevelType
                },
                success: function (data) {
                    layer.close(index);
                    if (data.status == 200) {
                        layer.msg("删除成功");
                        reLoad();
                    } else {
                        layer.msg(data.message);
                    }
                }
            });
        })
    }

    function isExpiredDate(date) {
        var nowDate = new Date();
        var endDate = new Date(date);
        if (nowDate.getTime() > endDate.getTime()) {
            return true;
        }
        return false;
    }

    function hlhtDiscount(hlhtOperatorId, tenantCode, hlhtTenantCode) {
        location.href = prefix + "/hlhtDiscount?hlhtOperatorId=" + hlhtOperatorId + "&tenantCode=" + tenantCode + "&hlhtTenantCode=" + hlhtTenantCode;
        ;
    }

    function exportHlhtTenantOpen() {
        $.fileDownload(prefix + "/exportHlhtTenantOpen", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text(),
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                hlhtOpenType: $("#hlhtOpenType").val(),
                hlhtType: $("#hlhtType").val(),
                status: $("#status").val(),
                hlhtTenantOrHlhtOperator: $("#hlhtTenantOrHlhtOperator").val()

            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        });
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        updateTenantOpenStatus: updateTenantOpenStatus,
        detailhlhtTenantOpen: detailhlhtTenantOpen,
        hlhtOrderInfo: hlhtOrderInfo,
        stationHlhtTenantOpen: stationHlhtTenantOpen,
        saveBatchHlhtTenantOpenStation: saveBatchHlhtTenantOpenStation,
        hlhtDiscount: hlhtDiscount,
        positiveHlhtStaion: positiveHlhtStaion,
        exportHlhtTenantOpen: exportHlhtTenantOpen,
        stationOpenfor: stationOpenfor
    };
})();


